
package Acciones.Persistencia;

import Configuraciones.Configuraciones;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.persistence.EntityManager;
import javax.swing.AbstractAction;
import javax.swing.JTable;
import javax.swing.KeyStroke;


public class AccionEliminar extends AbstractAction {
    private JTable tbl;
    private EntityManager em;
    private List ls;
    public static String NOMBRE="Eliminar";
    public static String DESCRIPCION="Eliminar los registros seleccionados de la tabla.";
    protected static int KEVENT=KeyEvent.VK_D;

    public AccionEliminar() {
            putValue(NAME, NOMBRE);
            putValue(SHORT_DESCRIPTION, DESCRIPCION);
            putValue(MNEMONIC_KEY, new Integer(KEVENT));
            putValue(ACCELERATOR_KEY, KeyStroke.getKeyStroke(KEVENT, 
                    Toolkit.getDefaultToolkit().getMenuShortcutKeyMask()));
            putValue(SMALL_ICON, Configuraciones.ICONO_ELIMINAR);
    }

    public AccionEliminar(JTable tbl, EntityManager em, List ls) {
        this();
        this.tbl = tbl;
        this.em = em;
        this.ls = ls;
    }
    
    
    
    

    @Override
    public void actionPerformed(ActionEvent e) {
        int [] selected = tbl.getSelectedRows();
        List<Serializable> toRemove= new ArrayList<Serializable>(selected.length);
        for (int i = 0; i < selected.length; i++) {
           Serializable u= (Serializable) ls.get(tbl.convertRowIndexToModel(selected[i]));
           toRemove.add(u);
            em.remove(u);
        }
        ls.removeAll(toRemove);
    }


    public JTable getTbl() {
        return tbl;
    }


    public void setTbl(JTable tbl) {
        this.tbl = tbl;
    }


    public EntityManager getEm() {
        return em;
    }

 
    public void setEm(EntityManager em) {
        this.em = em;
    }

 
    public List getLs() {
        return ls;
    }


    public void setLs(List ls) {
        this.ls = ls;
    }
    
}
